define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'money/coinorder/index' + location.search,
                    add_url: 'money/coinorder/add',
                    edit_url: 'money/coinorder/edit',
                    del_url: 'money/coinorder/del',
                    multi_url: 'money/coinorder/multi',
                    import_url: 'money/coinorder/import',
                    table: 'coin_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('用户ID')},
                        {field: 'user.mobile', title: __('用户手机号'), operate: 'LIKE'},
                        {field: 'order_no', title: __('订单编号'), operate: 'LIKE'},
                        {field: 'create_time', title: __('时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'amount', title: __('金额'), operate:'BETWEEN'},
                        {field: 'device_id', title: __('设备ID'), operate: 'LIKE'},
                        {field: 'status', title: __('状态'), searchList: {"待支付":__('Paying'),"payed":__('已支付')}, formatter: Table.api.formatter.status},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-del", null);
                                $(table).data("operate-edit", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                            buttons: [

                                {
                                    name: 'detail',
                                    title: __('金币详情'),
                                    text: "金币详情",
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-user-circle',
                                    url: function (row) {
                                        return "money/usercoin/index?order_id=" + row['id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('退款'),
                                    text: "退款",
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-bitcoin',
                                    confirm: '是否退款',
                                    url: function (row) {
                                        return "money/coinorder/doRefund?id=" + row['id'];
                                    },

                                    success: function (data, ret) {
                                        Layer.alert("退款成功 刷新查看结果");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }

                                }
                            ]
                        }]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
